Added tests
authorJeroen van der Heijden <jeroen@transceptor.technology>
Thu, 27 Sep 2018 09:33:29 +0000 (11:33 +0200)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Thu, 27 Sep 2018 09:33:29 +0000 (11:33 +0200)
34 files changed:
include/siri/cfg/cfg.h
include/strextra/strextra.h
include/xpath/xpath.h
src/procinfo/procinfo.c
src/siri/cfg/cfg.c
src/siri/db/db.c
src/siri/db/shards.c
src/siri/help/help.c
src/siri/net/pipe.c
src/xmath/xmath.c
src/xpath/xpath.c
test/test.h
test/test.sh
test/test_access/sources [new file with mode: 0644]
test/test_access/test_access.c [new file with mode: 0644]
test/test_aggr/sources [new file with mode: 0644]
test/test_aggr/test_aggr.c [new file with mode: 0644]
test/test_ctree/test_ctree.c
test/test_expr/sources [new file with mode: 0644]
test/test_expr/test_expr.c [new file with mode: 0644]
test/test_grammar/sources [new file with mode: 0644]
test/test_grammar/test_grammar.c [new file with mode: 0644]
test/test_imap/test_imap.c
test/test_iso8601/test_iso8601.c
test/test_slist/sources [new file with mode: 0644]
test/test_slist/test_slist.c [new file with mode: 0644]
test/test_strx/sources [new file with mode: 0644]
test/test_strx/test_strx.c [new file with mode: 0644]
test/test_version/sources [new file with mode: 0644]
test/test_version/test_version.c [new file with mode: 0644]
test/test_xmath/sources [new file with mode: 0644]
test/test_xmath/test_xmath.c [new file with mode: 0644]
test/test_xpath/sources [new file with mode: 0644]
test/test_xpath/test_xpath.c [new file with mode: 0644]

index d121283f97876d526445a2f4a88cf4e6b094eb3a..4cc44b192be5dbb196c37075d0f1fdc9ebfa4bff 100644 (file)
@@ -35,9 +35,9 @@ struct siri_cfg_s
     uint8_t ip_support;
     uint8_t shard_compression;
     char server_address[SIRI_CFG_MAX_LEN_ADDRESS];
-    char default_db_path[SIRI_PATH_MAX];
+    char default_db_path[XPATH_MAX];
     uint8_t pipe_support;
-    char pipe_client_name[SIRI_PATH_MAX];
+    char pipe_client_name[XPATH_MAX];
 };
 
 #endif  /* SIRI_CFG_H_ */
index 5334afabedfd99f003129e57c8fb62b813dab496..617155df22f3cfd96624b52d9e22132f37e8b74e 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <stdbool.h>
 #include <stddef.h>
+#include <inttypes.h>
 
 void strx_upper_case(char * sptr);
 void strx_lower_case(char * sptr);
index c6fe513c4e6404fa2f0d37a1ab2f04e63e2fb4ab..c2141fd840750ddd956d139d617b229f90234a36 100644 (file)
@@ -16,9 +16,9 @@
 #include <limits.h>
 
 #ifndef PATH_MAX
-#define SIRI_PATH_MAX 4096
+#define XPATH_MAX 4096
 #else
-#define SIRI_PATH_MAX PATH_MAX
+#define XPATH_MAX PATH_MAX
 #endif
 
 int xpath_file_exist(const char * fn);
index 6eb0bdd947f558be34b998ae144a10a18f45127e..4598332c603b563ac7050b5da63c28f25d3596a9 100644 (file)
@@ -161,8 +161,8 @@ long int procinfo_open_files(const char * path)
     DIR * dirp;
     struct dirent * entry;
     size_t len = strlen(path);
-    char buffer[SIRI_PATH_MAX];
-    char buf[SIRI_PATH_MAX];
+    char buffer[XPATH_MAX];
+    char buf[XPATH_MAX];
 
     if ((dirp = opendir("/proc/self/fd")) == NULL)
     {
@@ -173,7 +173,7 @@ long int procinfo_open_files(const char * path)
     {
         if (entry->d_type == DT_REG || entry->d_type == DT_LNK)
         {
-            snprintf(buffer, SIRI_PATH_MAX, "/proc/self/fd/%s", entry->d_name);
+            snprintf(buffer, XPATH_MAX, "/proc/self/fd/%s", entry->d_name);
 
             if (realpath(buffer, buf) == NULL)
             {
index 0f0ea70ee7377c02c082d97ac58e62fa11f39165..be7a5dffd5c8bbb9655715184cb73758a25a51d3 100644 (file)
@@ -393,13 +393,13 @@ static void SIRI_CFG_read_pipe_client_name(cfgparser_t * cfgparser)
     else
     {
         len = strlen(option->val->string);
-        if (len > SIRI_PATH_MAX-2)
+        if (len > XPATH_MAX-2)
         {
             log_warning(
                     "Pipe client name exceeds %d characters, please "
                     "check your configuration file: %s. "
                     "Using default value: '%s'",
-                    SIRI_PATH_MAX-2,
+                    XPATH_MAX-2,
                     siri.args->config,
                     siri_cfg.pipe_client_name);
         }
@@ -451,9 +451,9 @@ static void SIRI_CFG_read_default_db_path(cfgparser_t * cfgparser)
     }
     else
     {
-        memset(siri_cfg.default_db_path, 0, SIRI_PATH_MAX);
+        memset(siri_cfg.default_db_path, 0, XPATH_MAX);
 
-        if (strlen(option->val->string) >= SIRI_PATH_MAX -2 ||
+        if (strlen(option->val->string) >= XPATH_MAX -2 ||
             realpath(
                 option->val->string,
                 siri_cfg.default_db_path) == NULL)
@@ -467,17 +467,17 @@ static void SIRI_CFG_read_default_db_path(cfgparser_t * cfgparser)
             /* keep space left for a trailing slash and a terminator char */
             strncpy(siri_cfg.default_db_path,
                     option->val->string,
-                    SIRI_PATH_MAX - 2);
+                    XPATH_MAX - 2);
         }
 
         len = strlen(siri_cfg.default_db_path);
 
-        if (len == SIRI_PATH_MAX - 2)
+        if (len == XPATH_MAX - 2)
         {
             log_warning(
                     "Default database path exceeds %d characters, please "
                     "check your configuration file: %s",
-                    SIRI_PATH_MAX - 3,
+                    XPATH_MAX - 3,
                     siri.args->config);
         }
 
index b8dd764b566858bceee12a65e1c653ff106e3f8e..1d962cbac0577dd39412729f4d1dc9bba8c9231c 100644 (file)
@@ -91,9 +91,9 @@ int8_t siridb_get_idle_percentage(siridb_t * siridb)
  */
 int siridb_is_db_path(const char * dbpath)
 {
-    char buffer[SIRI_PATH_MAX];
+    char buffer[XPATH_MAX];
     snprintf(buffer,
-            SIRI_PATH_MAX,
+            XPATH_MAX,
             "%sdatabase.conf",
             dbpath);
     if (!xpath_file_exist(buffer))
@@ -101,7 +101,7 @@ int siridb_is_db_path(const char * dbpath)
         return 0;  /* false */
     }
     snprintf(buffer,
-            SIRI_PATH_MAX,
+            XPATH_MAX,
             "%sdatabase.dat",
             dbpath);
     if (!xpath_file_exist(buffer))
@@ -121,7 +121,7 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags)
 {
     size_t len = strlen(dbpath);
     lock_t lock_rc;
-    char buffer[SIRI_PATH_MAX];
+    char buffer[XPATH_MAX];
     cfgparser_t * cfgparser;
     cfgparser_option_t * option = NULL;
     qp_unpacker_t * unpacker;
@@ -167,7 +167,7 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags)
 
     /* read database.conf */
     snprintf(buffer,
-            SIRI_PATH_MAX,
+            XPATH_MAX,
             "%sdatabase.conf",
             dbpath);
 
@@ -186,7 +186,7 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags)
     }
 
     snprintf(buffer,
-            SIRI_PATH_MAX,
+            XPATH_MAX,
             "%sdatabase.dat",
             dbpath);
 
@@ -635,9 +635,9 @@ int siridb_open_files(siridb_t * siridb)
  */
 int siridb_save(siridb_t * siridb)
 {
-    char buffer[SIRI_PATH_MAX];
+    char buffer[XPATH_MAX];
     snprintf(buffer,
-            SIRI_PATH_MAX,
+            XPATH_MAX,
             "%sdatabase.dat",
             siridb->dbpath);
 
index 840372331dfcdeb73b02ff065b45fb78a5550cc7..365800ab68720d74abd7ae2dff8c3f5755940eb4 100644 (file)
@@ -46,7 +46,7 @@ int siridb_shards_load(siridb_t * siridb)
 {
     struct stat st;
     struct dirent ** shard_list;
-    char buffer[SIRI_PATH_MAX];
+    char buffer[XPATH_MAX];
     int n, total, rc = 0;
 
     memset(&st, 0, sizeof(struct stat));
@@ -55,7 +55,7 @@ int siridb_shards_load(siridb_t * siridb)
 
     siridb_misc_get_fn(path, siridb->dbpath, SIRIDB_SHARDS_PATH);
 
-    if (strlen(path) >= SIRI_PATH_MAX - SIRIDB_MAX_SHARD_FN_LEN - 1)
+    if (strlen(path) >= XPATH_MAX - SIRIDB_MAX_SHARD_FN_LEN - 1)
     {
         log_error("Shard path too long: '%s'", path);
         return -1;
@@ -86,7 +86,7 @@ int siridb_shards_load(siridb_t * siridb)
     {
         if (is_temp_fn(shard_list[n]->d_name))
         {
-            snprintf(buffer, SIRI_PATH_MAX, "%s%s",
+            snprintf(buffer, XPATH_MAX, "%s%s",
                    path, shard_list[n]->d_name);
 
             log_warning("Removing temporary file: '%s'", buffer);
index d4f87c363591e98609eb468d06bbf9fbc7157921..068fb35a159cccc78aeb8924b6719f47bb42eb2a 100644 (file)
@@ -37,8 +37,8 @@ const char * siri_help_get(
          * path must be initialized for xpath_get_exec_path to handle this variable
          * correctly.
          */
-        char path[SIRI_PATH_MAX];
-        char fn[SIRI_PATH_MAX];
+        char path[XPATH_MAX];
+        char fn[XPATH_MAX];
 
         memset(&path, 0, sizeof(path));
 
@@ -52,14 +52,14 @@ const char * siri_help_get(
             {
                 snprintf(
                         fn,
-                        SIRI_PATH_MAX,
+                        XPATH_MAX,
                         "%shelp/help%s.md",
                         path,
                         help_name + 1);
             }
             else
             {
-                snprintf(fn, SIRI_PATH_MAX, "%shelp/%s.md", path, help_name);
+                snprintf(fn, XPATH_MAX, "%shelp/%s.md", path, help_name);
             }
 
             log_debug("Reading help file: '%s'", fn);
index 0965209413b66353c1750d803ed38865a3913c1e..acf8b0f0dee4c5f51400c98678effebf96752082 100644 (file)
@@ -6,7 +6,7 @@
 #include <xpath/xpath.h>
 #include <logger/logger.h>
 
-#define PIPE_NAME_BUF_SZ SIRI_PATH_MAX
+#define PIPE_NAME_BUF_SZ XPATH_MAX
 
 /*
  * Return a name for the connection if successful or NULL in case of a failure.
index d7e3659daea663c03872c13e7dab2645aaeb15d2..328c72a04767c1cf248165a2a8103b81e48b7b4e 100644 (file)
@@ -23,7 +23,9 @@ uint32_t xmath_ipow(int base, int exp)
     while (exp)
     {
         if (exp & 1)
+        {
             result *= base;
+        }
         exp >>= 1;
         base *= base;
     }
index 0f7c8b3a414fbc587adc0824a7ea57f2ce1c2f1f..04c4e23772b857bc0fd2f308b48389ff359e8a71 100644 (file)
@@ -84,7 +84,7 @@ ssize_t xpath_get_content(char ** buffer, const char * fn)
 
 /*
  * Get the current executable path.
- * (path should at least have size SIRI_PATH_MAX)
+ * (path should at least have size XPATH_MAX)
  *
  * Returns 0 if successful or -1 in case of an error.
  * (this functions writes logging in case of errors)
@@ -93,7 +93,7 @@ int xpath_get_exec_path(char * path)
 {
     char* path_end;
 
-    if (readlink("/proc/self/exe", path, SIRI_PATH_MAX) == -1)
+    if (readlink("/proc/self/exe", path, XPATH_MAX) == -1)
     {
         log_critical("Cannot read executable path");
         return -1;
index 2ee193544caa80ed00dbd161119a1ec37322d29d..82b953328c8021f8d1b9b59c4ce9cda8d78948ca 100644 (file)
@@ -15,6 +15,7 @@ static struct timeval start;
 static struct timeval end;
 
 static int status = TEST_OK;
+static int count = 0;
 
 const char * padding =
         ".............................."
@@ -22,6 +23,7 @@ const char * padding =
 
 static void test_start(char * test_name)
 {
+    count = 0;
     int padlen = 60 - strlen(test_name);
     printf("Testing %s%*.*s", test_name, padlen, padlen, padding);
     gettimeofday(&start, 0);
@@ -40,6 +42,6 @@ static int test_end(void)
     return status;
 }
 
-#define _assert(e) (void)((e)?0:(status = TEST_FAILED) && printf("\n\x1B[33mAssertion failed (%s:%d):\x1B[0m %s\n\n", __FILE__, __LINE__, #e))
+#define _assert(e) (void)((e)?count++:(status = TEST_FAILED) && printf("\n\x1B[33mAssertion failed (%s:%d):\x1B[0m %s\n\n", __FILE__, __LINE__, #e))
 
 #endif  /* SIRIDB_TEST_H_ */
\ No newline at end of file
index e6629766bceed6a33bb6b6f3697e1921bc8e850d..106003fb663655937485c937950d6ae76607b380 100755 (executable)
@@ -1,5 +1,13 @@
 #!/bin/bash
 RET=0
+echo -n "Test using valgrind for memory errors and leaks: "
+if [[ "$NOMEMTEST" -ne "1" ]] && hash valgrind 2>/dev/null; then
+    NOMEMTEST=0;
+    echo -e "\x1B[32menabled\x1B[0m";
+else
+    NOMEMTEST=1;
+    echo -e "\x1B[33mdisabled\x1B[0m";
+fi
 
 run () {
     C_SRC=$(cat $1/sources)
@@ -8,9 +16,8 @@ run () {
     OUT=$1.out
     rm "$OUT" 2> /dev/null
 
-    gcc -I"../include" -O0 -g3 -Wall -Wextra -Winline -std=gnu99 $SOURCE $C_SRC -o "$OUT"
-    if [[ "$NOMEMTEST" -ne "1" ]] && hash valgrind 2>/dev/null; then
-        echo -n "(memtest) "
+    gcc -I"../include" -O0 -g3 -Wall -Wextra -Winline -std=gnu99 $SOURCE $C_SRC -lm -lpcre2-8 -lcleri -o "$OUT"
+    if [[ "$NOMEMTEST" -ne "1" ]]; then
         valgrind --tool=memcheck --error-exitcode=1 --leak-check=full -q ./$OUT
     else
         ./$OUT
@@ -24,7 +31,8 @@ if [ $# -eq 0 ]; then
         run "${d%?}"
     done
 else
-    run "$1"
+    name=`echo $1 | sed 's/\(test_\)\?\(.*\?\)$/\2/g' | sed 's/\(.*\)\/$/\1/g'`
+    run "test_$name"
 fi
 
 exit $RET
\ No newline at end of file
diff --git a/test/test_access/sources b/test/test_access/sources
new file mode 100644 (file)
index 0000000..16d38cd
--- /dev/null
@@ -0,0 +1 @@
+../src/siri/db/access.c
diff --git a/test/test_access/test_access.c b/test/test_access/test_access.c
new file mode 100644 (file)
index 0000000..bca9e19
--- /dev/null
@@ -0,0 +1,39 @@
+#include "../test.h"
+#include <siri/db/access.h>
+
+
+int main()
+{
+    test_start("access");
+
+    char buffer[SIRIDB_ACCESS_STR_MAX];
+    uint32_t access_bit = 0;
+
+    siridb_access_to_str(buffer, access_bit);
+    _assert (strcmp(buffer, "no access") == 0);
+
+    access_bit |= SIRIDB_ACCESS_SHOW;
+    siridb_access_to_str(buffer, access_bit);
+    _assert (strcmp(buffer, "show") == 0);
+
+    access_bit |= SIRIDB_ACCESS_SELECT;
+    siridb_access_to_str(buffer, access_bit);
+    _assert (strcmp(buffer, "select and show") == 0);
+
+    access_bit |= SIRIDB_ACCESS_LIST;
+    siridb_access_to_str(buffer, access_bit);
+    _assert (strcmp(buffer, "list, select and show") == 0);
+
+    access_bit |= SIRIDB_ACCESS_PROFILE_WRITE;
+    siridb_access_to_str(buffer, access_bit);
+    _assert (strcmp(buffer, "write") == 0);
+
+    access_bit &= ~SIRIDB_ACCESS_INSERT;
+    siridb_access_to_str(buffer, access_bit);
+    _assert (strcmp(buffer, "read and create") == 0);
+
+    _assert (siridb_access_from_strn("read", 4) == (SIRIDB_ACCESS_PROFILE_READ));
+    _assert (siridb_access_from_strn("list", 4) == SIRIDB_ACCESS_LIST);
+
+    return test_end();
+}
\ No newline at end of file
diff --git a/test/test_aggr/sources b/test/test_aggr/sources
new file mode 100644 (file)
index 0000000..0e8f9f4
--- /dev/null
@@ -0,0 +1,11 @@
+../src/siri/db/aggregate.c
+../src/siri/db/points.c
+../src/siri/db/variance.c
+../src/siri/db/median.c
+../src/siri/db/re.c
+../src/siri/err.c
+../src/qpack/qpack.c
+../src/slist/slist.c
+../src/cexpr/cexpr.c
+../src/strextra/strextra.c
+../src/logger/logger.c
diff --git a/test/test_aggr/test_aggr.c b/test/test_aggr/test_aggr.c
new file mode 100644 (file)
index 0000000..f4e54e0
--- /dev/null
@@ -0,0 +1,389 @@
+#include <math.h>
+#include "../test.h"
+#include <siri/db/points.h>
+#include <siri/db/aggregate.h>
+
+
+#define SIRIDB_MAX_SIZE_ERR_MSG 1024
+
+static siridb_aggr_t aggr;
+static char err_msg[SIRIDB_MAX_SIZE_ERR_MSG];
+
+
+static siridb_points_t * prepare_points(void)
+{
+    siridb_points_t * points = siridb_points_new(10, TP_INT);
+    uint64_t timestamps[10] =   {3, 6, 7, 10, 11, 13, 14, 15, 25, 27};
+    int64_t values[10] =        {1, 3, 0, 2,  4,  8,  3,  5,  6,  3};
+    qp_via_t val;
+    unsigned int i;
+
+    siridb_init_aggregates();
+
+    for (i = 0; i < 10; i++)
+    {
+        val.int64 = values[i];
+        siridb_points_add_point(points, &timestamps[i], &val);
+    }
+
+    return points;
+}
+
+static int test_count(void)
+{
+    test_start("aggr (count)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_COUNT;
+    aggr.group_by = 6;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 4);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 6 && aggrp->data->val.int64 == 2);
+    _assert ((aggrp->data + 3)->ts == 30 &&
+            (aggrp->data + 3)->val.int64 == 2);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_first(void)
+{
+    test_start("aggr (first)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_FIRST;
+    aggr.group_by = 5;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 5);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 5 && aggrp->data->val.int64 == 1);
+    _assert ((aggrp->data + 2)->ts == 15 &&
+            (aggrp->data + 2)->val.int64 == 4);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_last(void)
+{
+    test_start("aggr (last)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_LAST;
+    aggr.group_by = 5;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 5);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 5 && aggrp->data->val.int64 == 1);
+    _assert ((aggrp->data + 2)->ts == 15 &&
+            (aggrp->data + 2)->val.int64 == 5);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_max(void)
+{
+    test_start("aggr (max)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_MAX;
+    aggr.group_by = 10;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 3);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 10 && aggrp->data->val.int64 == 3);
+    _assert ((aggrp->data + 2)->ts == 30 &&
+            (aggrp->data + 2)->val.int64 == 6);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+
+static int test_mean(void)
+{
+    test_start("aggr (mean)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_MEAN;
+    aggr.group_by = 4;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 5);
+    _assert (aggrp->tp == TP_DOUBLE);
+    _assert (aggrp->data->ts == 4 && aggrp->data->val.real == 1.0);
+    _assert ((aggrp->data + 4)->ts == 28 &&
+            (aggrp->data + 4)->val.real == 4.5);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_median(void)
+{
+    test_start("aggr (median)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_MEDIAN;
+    aggr.group_by = 7;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 4);
+    _assert (aggrp->tp == TP_DOUBLE);
+    _assert (aggrp->data->ts == 7 && aggrp->data->val.real == 1.0);
+    _assert ((aggrp->data + 1)->ts == 14 &&
+            (aggrp->data + 1)->val.real == 3.5);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_median_high(void)
+{
+    test_start("aggr (median_high)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_MEDIAN_HIGH;
+    aggr.group_by = 7;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 4);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 7 && aggrp->data->val.int64 == 1);
+    _assert ((aggrp->data + 1)->ts == 14 &&
+            (aggrp->data + 1)->val.int64 == 4);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_median_low(void)
+{
+    test_start("aggr (median_low)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_MEDIAN_LOW;
+    aggr.group_by = 7;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 4);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 7 && aggrp->data->val.int64 == 1);
+    _assert ((aggrp->data + 1)->ts == 14 &&
+            (aggrp->data + 1)->val.int64 == 3);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_min(void)
+{
+    test_start("aggr (min)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_MIN;
+    aggr.group_by = 2;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 9);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 4 && aggrp->data->val.int64 == 1);
+    _assert ((aggrp->data + 5)->ts == 14 &&
+            (aggrp->data + 5)->val.int64 == 3);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_pvariance(void)
+{
+    test_start("aggr (pvariance)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_PVARIANCE;
+    aggr.group_by = 5;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 5);
+    _assert (aggrp->tp == TP_DOUBLE);
+    _assert (aggrp->data->ts == 5 && aggrp->data->val.real == 0.0);
+    _assert ((aggrp->data + 2)->ts == 15 &&
+            (aggrp->data + 2)->val.real == 3.5);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_stddev(void)
+{
+    test_start("aggr (stddev)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_STDDEV;
+    aggr.group_by = 6;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 4);
+    _assert (aggrp->tp == TP_DOUBLE);
+    _assert (aggrp->data->ts == 6 && aggrp->data->val.real == sqrt(2.0));
+    _assert ((aggrp->data + 1)->ts == 12 &&
+            (aggrp->data + 1)->val.real == 2.0);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_sum(void)
+{
+    test_start("aggr (sum)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_SUM;
+    aggr.group_by = 5;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 5);
+    _assert (aggrp->tp == TP_INT);
+    _assert (aggrp->data->ts == 5 && aggrp->data->val.int64 == 1);
+    _assert ((aggrp->data + 2)->ts == 15 &&
+            (aggrp->data + 2)->val.int64 == 20);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+static int test_variance(void)
+{
+    test_start("aggr (variance)");
+
+    siridb_points_t * aggrp, * points = prepare_points();
+
+    aggr.gid = CLERI_GID_F_VARIANCE;
+    aggr.group_by = 6;
+    aggr.limit = 0;
+    aggr.offset = 0;
+
+    aggrp = siridb_aggregate_run(points, &aggr, err_msg);
+
+    _assert (aggrp != NULL);
+    _assert (aggrp->len == 4);
+    _assert (aggrp->tp == TP_DOUBLE);
+    _assert (aggrp->data->ts == 6 && aggrp->data->val.real == 2.0);
+    _assert ((aggrp->data + 1)->ts == 12 &&
+            (aggrp->data + 1)->val.real == 4.0);
+
+    siridb_points_free(aggrp);
+    siridb_points_free(points);
+
+    return test_end();
+}
+
+int main()
+{
+    return (
+        test_count() ||
+        test_first() ||
+        test_last() ||
+        test_max() ||
+        test_mean() ||
+        test_median() ||
+        test_median_high() ||
+        test_median_low() ||
+        test_min() ||
+        test_pvariance() ||
+        test_stddev() ||
+        test_sum() ||
+        test_variance() ||
+        0
+    );
+}
index 39b6024ae9e1facef909b3e0cccf5a814813e039..27e12e2a129176cc474e0605207636e5a3a6d39d 100644 (file)
@@ -1,11 +1,3 @@
-/*
- * test_smap.c
- *
- *  Created on: Sep 30, 2017
- *      Author: Jeroen van der Heijden <jeroen@transceptor.technology>
- */
-
-
 #include "../test.h"
 #include <ctree/ctree.h>
 
@@ -35,8 +27,9 @@ int main()
 
     /* test adding values */
     {
+        unsigned int i;
         _assert (ctree->len == 0);
-        for (unsigned int i = 0; i < num_entries; i++)
+        for (i = 0; i < num_entries; i++)
         {
             _assert (ct_add(ctree, entries[i], entries[i]) == 0);
         }
@@ -49,7 +42,8 @@ int main()
 
     /* test adding duplicated values */
     {
-        for (unsigned int i = 0; i < num_entries; i++)
+        unsigned int i;
+        for (i = 0; i < num_entries; i++)
         {
             _assert (ct_add(ctree, entries[i], entries[i]) == CT_EXISTS);
         }
@@ -62,7 +56,8 @@ int main()
 
     /* test get */
     {
-        for (unsigned int i = 0; i < num_entries; i++)
+        unsigned int i;
+        for (i = 0; i < num_entries; i++)
         {
             _assert (ct_get(ctree, entries[i]) == entries[i]);
         }
@@ -70,7 +65,8 @@ int main()
 
     /* test getn */
     {
-        for (unsigned int i = 0; i < num_entries; i++)
+        unsigned int i;
+        for (i = 0; i < num_entries; i++)
         {
             _assert (ct_getn(
                 ctree,
@@ -81,7 +77,8 @@ int main()
 
     /* test pop value */
     {
-        for (unsigned int i = 0; i < num_entries; i++)
+        unsigned int i;
+        for (i = 0; i < num_entries; i++)
         {
             _assert (ct_pop(ctree, entries[i]) == entries[i]);
         }
diff --git a/test/test_expr/sources b/test/test_expr/sources
new file mode 100644 (file)
index 0000000..6891284
--- /dev/null
@@ -0,0 +1 @@
+../src/expr/expr.c
diff --git a/test/test_expr/test_expr.c b/test/test_expr/test_expr.c
new file mode 100644 (file)
index 0000000..eca052d
--- /dev/null
@@ -0,0 +1,26 @@
+#include "../test.h"
+#include <expr/expr.h>
+
+
+int main()
+{
+    test_start("expr");
+
+    int64_t result;
+
+    _assert(expr_parse(&result, "5+37") == 0 && result == 42);
+    _assert(expr_parse(&result, "2+2*20") == 0 && result == 42);
+    _assert(expr_parse(&result, "(2+4)*7") == 0 && result == 42);
+    _assert(expr_parse(&result, "16%10*7") == 0 && result == 42);
+    _assert(expr_parse(&result, "7*14%56") == 0 && result == 42);
+    _assert(expr_parse(&result, "21/3*6") == 0 && result == 42);
+    _assert(expr_parse(&result, "22/3*6") == 0 && result == 42);
+
+    /* division by zero is not a good idea */
+    _assert(expr_parse(&result, "42/(2-2)") == EXPR_DIVISION_BY_ZERO);
+
+    /* module by zero is not a good idea either */
+    _assert(expr_parse(&result, "42%(2-2)") == EXPR_MODULO_BY_ZERO);
+
+    return test_end();
+}
diff --git a/test/test_grammar/sources b/test/test_grammar/sources
new file mode 100644 (file)
index 0000000..1038a9a
--- /dev/null
@@ -0,0 +1 @@
+../src/siri/grammar/grammar.c
diff --git a/test/test_grammar/test_grammar.c b/test/test_grammar/test_grammar.c
new file mode 100644 (file)
index 0000000..e5190df
--- /dev/null
@@ -0,0 +1,41 @@
+#include "../test.h"
+#include <siri/grammar/grammar.h>
+#include <siri/grammar/gramp.h>
+
+#define assert_valid(__g, __q)(_assert(_is_valid(__g, __q)))
+#define assert_invalid(__g, __q)(_assert(!_is_valid(__g, __q)))
+
+static int _is_valid(cleri_grammar_t * grammar, char * query)
+{
+    cleri_parse_t * pr = cleri_parse(grammar, query);
+    _assert (pr);
+    int is_valid = pr->is_valid;
+    cleri_parse_free(pr);
+    return is_valid;
+}
+
+int main()
+{
+    test_start("grammar");
+
+    cleri_grammar_t * grammar = compile_grammar();
+
+    assert_invalid(grammar, "select * from");
+    assert_invalid(grammar, "list");
+
+    assert_valid(grammar, "");
+    assert_valid(grammar, "now - 1w");
+    assert_valid(grammar, "help # with comments");
+    assert_valid(grammar, "select * from *");
+    assert_valid(grammar, "select * from 'series'");
+    assert_valid(grammar, "select * from * after now-1d");
+    assert_valid(grammar, "list series");
+    assert_valid(grammar,
+        "select mean(1h + 1m) from \"series-001\", \"series-002\", "
+        "\"series-003\" between 1360152000 and 1360152000 + 1d merge as "
+        "\"series\" using mean(1)");
+
+    cleri_grammar_free(grammar);
+
+    return test_end();
+}
index 24d3f06498185ce34b161b9a8cc98f89187d4471..00db4c43dc7311dc50d7347f3212f3d0e5408293 100644 (file)
@@ -1,11 +1,3 @@
-/*
- * test_smap.c
- *
- *  Created on: Sep 30, 2017
- *      Author: Jeroen van der Heijden <jeroen@transceptor.technology>
- */
-
-
 #include "../test.h"
 #include <imap/imap.h>
 
index fb7ccf4c4b25de8ec76766efe6fedfaa244ff4e7..ddd3a724f22a98c8b1d3784ce4eb7da809023874 100644 (file)
@@ -1,11 +1,5 @@
-/*
- * test_smap.c
- *
- *  Created on: Sep 30, 2017
- *      Author: Jeroen van der Heijden <jeroen@transceptor.technology>
- */
-
-
+#include <time.h>
+#include <locale.h>
 #include "../test.h"
 #include <iso8601/iso8601.h>
 
@@ -14,6 +8,11 @@ int main()
 {
     test_start("iso8601");
 
+    /* Update local and timezone */
+    (void) setlocale(LC_ALL, "");
+    putenv("TZ=:UTC");
+    tzset();
+
     iso8601_tz_t amsterdam = iso8601_tz("Europe/Amsterdam");
     _assert(amsterdam > 0);
 
@@ -26,7 +25,6 @@ int main()
     _assert(iso8601_tz("Ams") < 0);
 
     /* Test parsing a year with time-zone information */
-    printf("%ld", iso8601_parse_date("2013", amsterdam));
     _assert(iso8601_parse_date("2013", amsterdam) == 1356994800);
 
     /* Customer offset should be preferred over UTC */
diff --git a/test/test_slist/sources b/test/test_slist/sources
new file mode 100644 (file)
index 0000000..7a34516
--- /dev/null
@@ -0,0 +1,2 @@
+../src/slist/slist.c
+../src/logger/logger.c
\ No newline at end of file
diff --git a/test/test_slist/test_slist.c b/test/test_slist/test_slist.c
new file mode 100644 (file)
index 0000000..ef5c1b0
--- /dev/null
@@ -0,0 +1,78 @@
+#include "../test.h"
+#include <slist/slist.h>
+
+const unsigned int num_entries = 14;
+char * entries[] = {
+    "Zero",
+    "First entry",
+    "Second entry",
+    "Third entry",
+    "Fourth entry",
+    "Fifth entry",
+    "Sixth entry",
+    "Seventh entry",
+    "8",
+    "9",
+    "entry 10",
+    "entry 11",
+    "entry 12",
+    "entry-last"
+};
+
+int main()
+{
+    test_start("slist");
+
+    /* slist_append_safe */
+    {
+        slist_t * slist = slist_new(0);
+        _assert (slist->len == 0);
+        _assert (slist->size == 0);
+
+        unsigned int i;
+        for (i = 0; i < num_entries; i++)
+        {
+            _assert (slist_append_safe(&slist, entries[i]) == 0);
+        }
+
+        /* slist_copy */
+        {
+            slist_t * slistcp = slist_copy(slist);
+            unsigned int i;
+            for (i = 0; i < num_entries; i++)
+            {
+                _assert (slistcp->data[i] == entries[i]);
+            }
+            slist_free(slistcp);
+        }
+
+        _assert (slist->len == num_entries);
+        slist_free(slist);
+    }
+
+    /* slist_append */
+    {
+        slist_t * slist = slist_new(num_entries);
+        _assert (slist->len == 0);
+        _assert (slist->size == num_entries);
+
+        unsigned int i;
+        for (i = 0; i < num_entries; i++)
+        {
+            slist_append(slist, entries[i]);
+        }
+
+        _assert (slist->len == num_entries);
+
+        /* slist_pop */
+        for (i = num_entries; i-- > 0;)
+        {
+            _assert (slist_pop(slist) == entries[i]);
+        }
+
+        slist_free(slist);
+    }
+
+
+    return test_end();
+}
diff --git a/test/test_strx/sources b/test/test_strx/sources
new file mode 100644 (file)
index 0000000..f82c483
--- /dev/null
@@ -0,0 +1 @@
+../src/strextra/strextra.c
diff --git a/test/test_strx/test_strx.c b/test/test_strx/test_strx.c
new file mode 100644 (file)
index 0000000..8ddc7e5
--- /dev/null
@@ -0,0 +1,21 @@
+#include "../test.h"
+#include <strextra/strextra.h>
+
+
+int main()
+{
+    test_start("strx");
+
+    /* strx_to_double */
+    {
+        _assert (strx_to_double("0.5", 3) == 0.5);
+        _assert (strx_to_double("0.55", 3) == 0.5);
+        _assert (strx_to_double("123.456", 7) == 123.456);
+        _assert (strx_to_double("123", 3) == 123);
+        _assert (strx_to_double("123.", 4) == 123);
+        _assert (strx_to_double("123456.", 3) == 123);
+        _assert (strx_to_double("-0.5", 3) == -0.5);
+    }
+
+    return test_end();
+}
diff --git a/test/test_version/sources b/test/test_version/sources
new file mode 100644 (file)
index 0000000..280e66a
--- /dev/null
@@ -0,0 +1 @@
+../src/siri/version.c
diff --git a/test/test_version/test_version.c b/test/test_version/test_version.c
new file mode 100644 (file)
index 0000000..85552d2
--- /dev/null
@@ -0,0 +1,23 @@
+#include "../test.h"
+#include <siri/version.h>
+
+
+int main()
+{
+    test_start("version");
+
+    _assert (siri_version_cmp("1.0.0", "2.0.0") < 0);
+    _assert (siri_version_cmp("2.0.0", "1.0.0") > 0);
+    _assert (siri_version_cmp("2.2.0", "2.32.0") < 0);
+    _assert (siri_version_cmp("2.32.0", "2.2.0") > 0);
+    _assert (siri_version_cmp("2.0.5", "2.0.22") < 0);
+    _assert (siri_version_cmp("2.0.22", "2.0.5") > 0);
+    _assert (siri_version_cmp("2.0", "2.0.0") < 0);
+    _assert (siri_version_cmp("2.0.2", "2.0") > 0);
+    _assert (siri_version_cmp("a", "") > 0);
+    _assert (siri_version_cmp("", "b") < 0);
+    _assert (siri_version_cmp("", "") == 0);
+    _assert (siri_version_cmp("2.0.0", "2.0.0") == 0);
+
+    return test_end();
+}
diff --git a/test/test_xmath/sources b/test/test_xmath/sources
new file mode 100644 (file)
index 0000000..263b9c7
--- /dev/null
@@ -0,0 +1 @@
+../src/xmath/xmath.c
diff --git a/test/test_xmath/test_xmath.c b/test/test_xmath/test_xmath.c
new file mode 100644 (file)
index 0000000..4e8e519
--- /dev/null
@@ -0,0 +1,26 @@
+#include "../test.h"
+#include <xmath/xmath.h>
+
+
+int main()
+{
+    test_start("xmath");
+
+    /* xmath_ipow */
+    {
+        _assert (xmath_ipow(1000, 0) == 1);
+        _assert (xmath_ipow(1000, 1) == 1000);
+        _assert (xmath_ipow(1000, 2) == 1000000);
+        _assert (xmath_ipow(1000, 3) == 1000000000);
+        _assert (xmath_ipow(2, 8) == 256);
+    }
+
+    /* xmath_max_size */
+    {
+        _assert (xmath_max_size(3, 10, 20, 30) == 30);
+        _assert (xmath_max_size(3, 30, 20, 10) == 30);
+        _assert (xmath_max_size(3, 10, 30, 20) == 30);
+    }
+
+    return test_end();
+}
diff --git a/test/test_xpath/sources b/test/test_xpath/sources
new file mode 100644 (file)
index 0000000..2bfe07e
--- /dev/null
@@ -0,0 +1,2 @@
+../src/xpath/xpath.c
+../src/logger/logger.c
\ No newline at end of file
diff --git a/test/test_xpath/test_xpath.c b/test/test_xpath/test_xpath.c
new file mode 100644 (file)
index 0000000..6b09407
--- /dev/null
@@ -0,0 +1,16 @@
+#include "../test.h"
+#include <xpath/xpath.h>
+
+
+int main()
+{
+    test_start("xpath");
+
+    /* xpath_file_exist */
+    {
+        _assert (xpath_file_exist("./test.h"));
+        _assert (!xpath_file_exist("./test.foo"));
+    }
+
+    return test_end();
+}